package com.sgl.blog.controller.blog;

import com.sgl.blog.pojo.*;
import com.sgl.blog.service.CommentService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

@RestController
@RequestMapping("blog")
public class CommentController {

    @Resource
    private CommentService commentService;


    /* get和post请求差别：用post请求必须有@RequestBody，并且Ajax必须有 contentType: "application/json;charset=UTF-8", data : JSON.stringify({commentContent:content,articleId:id}),
    get请求不能用@RequestBody，并且data用{"commentContent":content,"articleId":id}
    @GetMapping("comment")
        public Restful insert(BlogComment comment){

            System.out.println(comment);
            return null;
        }*/

    /**
     * 添加评论
     * @param comment
     * @return
     */
    @PostMapping("comment")
    public Restful insert(@RequestBody BlogComment comment, HttpSession session){
        System.out.println(1);
        BlogAccount account = (BlogAccount) session.getAttribute("account");
        if (account != null){
            System.out.println(2);
            comment.setAccountId(account.getAccountId());
            return RestfulBuilder.success(commentService.insertComment(comment),ResultCode.SUCCESS);
        }
        return RestfulBuilder.faile(ResultCode.USER_NOT_LOGGED_IN);
    }

    /**
     * 查询所有评论
     * @return
     */
    @GetMapping("comment/{id}")
    public Restful find(@PathVariable Integer id){
        return RestfulBuilder.success(commentService.findByArticleId(id),ResultCode.SUCCESS);
    }


}
